/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.code.sapien.dao.CategoryAclDAO;
import com.google.code.sapien.dao.CategoryDAO;
import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.CategoryAcl;
import com.google.code.sapien.model.Role;
import com.google.code.sapien.model.User;
import com.google.inject.Inject;

/**
 * Default implementation of the {@link com.google.code.sapien.service.CategoryService}.
 * @author Adam
 * @version $Id: CategorySapienService.java 25 2009-05-26 04:23:01Z a.ruggles $
 * 
 * Created on Feb 10, 2009 at 7:26:34 PM 
 */
public class CategorySapienService implements CategoryService {

	/**
	 * The category ACL data access object.
	 */
	private final CategoryAclDAO categoryAclDAO;

	/**
	 * The category data access object.
	 */
	private final CategoryDAO categoryDAO;

	/**
	 * Constructs a category sapien service.
	 * @param categoryDAO The category data access object.
	 * @param categoryAclDAO The category ACL data access object.
	 */
	@Inject
	public CategorySapienService(final CategoryDAO categoryDAO, final CategoryAclDAO categoryAclDAO) {
		this.categoryDAO = categoryDAO;
		this.categoryAclDAO = categoryAclDAO;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#add(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public void add(final Category entity, final User creator) {
		entity.setDiscussionCount(0);
		entity.setLastDiscussion(null);
		entity.setMessageCount(0);
		entity.setOrder(categoryDAO.getCount());
		categoryDAO.add(entity);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#get(java.io.Serializable)
	 */
	public Category get(final Serializable id) {
		return categoryDAO.get(id);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryService#getAll()
	 */
	public List<Category> getAll() {
		return categoryDAO.listAll();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryService#getPermissions(com.google.code.sapien.model.Category)
	 */
	public List<CategoryAcl> getPermissions(final Category category) {
		List<CategoryAcl> acls = new ArrayList<CategoryAcl>();
		if (category != null && category.getId() != null) {
			acls = categoryAclDAO.list(category);
		}
		return acls;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryService#getPermissions(com.google.code.sapien.model.Role)
	 */
	public List<CategoryAcl> getPermissions(final Role role) {
		List<CategoryAcl> acls = new ArrayList<CategoryAcl>();
		if (role != null && role.getId() != null) {
			acls = categoryAclDAO.list(role);
		}
		return acls;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#remove(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public void remove(final Category entity, final User remover) {
		final Category category = categoryDAO.load(entity.getId());
		categoryDAO.remove(category);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#update(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public Category update(final Category entity, final User updator) {
		final Category category = categoryDAO.load(entity.getId());
		if (entity.getName() != null) {
			category.setName(entity.getName());
		}
		if (entity.getDescription() != null) {
			category.setDescription(entity.getDescription());
		}
		if (entity.getLastDiscussion() != null) {
			category.setLastDiscussion(entity.getLastDiscussion());
		}
		if (entity.getMessageCount() != null) {
			category.setMessageCount(entity.getMessageCount());
		}
		if (entity.getDiscussionCount() != null) {
			category.setDiscussionCount(entity.getDiscussionCount());
		}
		if (entity.getOrder() != null) {
			category.setOrder(entity.getOrder());
		}
		return category;
	}

}
